package com.example.wifi_scan;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;

import java.io.IOException;
import java.util.List;

public class scanwifi extends Thread {

    private int i = 0;
    private WifiManager wifiManager;
    private Handler handler;

    private RSSI rssi = new RSSI(
            resource.maclist[0],
            resource.maclist[1],
            resource.maclist[2]
    );

    private static final int COMPLETED = 0;
    public static final int END = 1;

    //get和set方法
    public WifiManager getWifiManager() {
        return wifiManager;
    }

    public void setWifiManager(WifiManager wifiManager) {
        this.wifiManager = wifiManager;
    }

    //构造函数
    public scanwifi(WifiManager wifiManager, String number, Handler handler) {
        this.wifiManager = wifiManager;
        this.handler = handler;

    }

    @Override
    public void run() {

        int time = 10; //次数，不是时间
        while (time > 0) { //共扫描10次
            try {
                showwifilist();
                Thread.currentThread().sleep(1000); //每隔1秒扫描一次
                --time;
            } catch (InterruptedException | IOException e) {
                e.printStackTrace();
            }
        }

        Message msg = new Message();
        msg.what = END;
        handler.sendMessage(msg);
    }

    //扫描函数
    public void showwifilist() throws IOException {
        //新声明一个字符串缓冲区
        StringBuffer tempbuffer = new StringBuffer();

        //扫描列表，获取wifi列表
        wifiManager.startScan();
        List<ScanResult> results = wifiManager.getScanResults();

        //对扫描结果进行处理
        if (results.isEmpty())
            tempbuffer.append("!获取wifi列表失败\n");
        else {
            tempbuffer.append("\n获取wifi列表成功：\n");
            for (ScanResult result : results) {
                tempbuffer.append(result.BSSID + ":" + result.level + "DBm\n");

                //进行判断，将获取到的rssi值存入到RSSI中
                if (result.BSSID.equals(rssi.getAP1MAC())) {
                    rssi.setAP1RSSI(result.level);
                }
                if(result.BSSID.equals(rssi.getAP2MAC())){
                    rssi.setAP2RSSI(result.level);
                }
                if(result.BSSID.equals(rssi.getAP3MAC())){
                    rssi.setAP3RSSI(result.level);
                }

            }
        }
        tempbuffer.append(i++);
        tempbuffer.append("\n");

        resource.stringBuffer = tempbuffer;
        //尝试发送数据给服务器
        new SendDateToServer(handler).getAsyn(rssi,1);

        //new SendDateToServer(handler).SendDataToServer("test", "test");

        Message msg = new Message();
        msg.what = COMPLETED;
        handler.sendMessage(msg);
    }

    public void showWifiListButNotSend() throws IOException {
        //新声明一个字符串缓冲区
        StringBuffer tempbuffer = new StringBuffer();

        //扫描列表，获取wifi列表
        wifiManager.startScan();
        List<ScanResult> results = wifiManager.getScanResults();

        //对扫描结果进行处理
        if (results.isEmpty())
            tempbuffer.append("!获取wifi列表失败\n");
        else {
            tempbuffer.append("\n获取wifi列表成功：\n");
            for (ScanResult result : results) {
                tempbuffer.append(result.BSSID + ":" + result.level + "DBm\n");

                //进行判断，将获取到的rssi值存入到RSSI中
                if (result.BSSID.equals(rssi.getAP1MAC())) {
                    rssi.setAP1RSSI(result.level);
                }
                if(result.BSSID.equals(rssi.getAP2MAC())){
                    rssi.setAP2RSSI(result.level);
                }
                if(result.BSSID.equals(rssi.getAP3MAC())){
                    rssi.setAP3RSSI(result.level);
                }

            }
        }
        tempbuffer.append(i++);
        tempbuffer.append("\n");
        resource.stringBuffer = tempbuffer;

        Message msg = new Message();
        msg.what = COMPLETED;
        handler.sendMessage(msg);
    }
}

